package service

import (
	"context"
	__ "mt-server/basic/proto"
	"mt-server/handler/model"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	__.UnimplementedMtServer
}

// 用户下单
func (s *Server) AddOrder(_ context.Context, in *__.AddOrderReq) (*__.AddOrderResp, error) {
	var err error
	var order model.Order

	//参数校验
	if in.Amount == 0 || in.Address == "" {
		return &__.AddOrderResp{
			Msg:  "参数不能为空",
			Code: 500,
		}, nil
	}

	order = model.Order{
		Amount:  float64(in.Amount),
		Address: in.Address,
		UserId:  int32(in.UserId),
		GoodsId: int32(in.GoodId),
	}
	err = order.AddOrder()
	if err != nil {
		return &__.AddOrderResp{
			Msg:  "用户下单失败",
			Code: 500,
		}, nil
	}

	return &__.AddOrderResp{
		Msg:  "用户下单成功",
		Code: 200,
	}, nil

}
